package com.excu.factory.demo;


import com.excu.factory.demo.base.BaseFactory;
import com.excu.factory.entity.annotation.auto.Column;
import com.excu.factory.inter.TableFactory;
import com.excu.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Check0bjFactory extends BaseFactory implements TableFactory {
 //  private static Map<TableEntity, ColumnEntity> map;
   public static Check0bjFactory getInstance(){
    /*  if (map==null){
         map = new ConcurrentHashMap<>();
      }*/
      return new Check0bjFactory();
   }
    public static String getInstance(Object obj, Map<String,String> params){
        Check0bjFactory check0BjFactory = new Check0bjFactory();
        return check0BjFactory.checkNotBlankObj(obj, params);
    }

    public static String getInstance(Object obj){
        Check0bjFactory check0BjFactory = new Check0bjFactory();
        return  check0BjFactory.checkObj(obj);
    }
    public static String getInstance(Object obj, List<String> nameList){
        Check0bjFactory check0BjFactory = new Check0bjFactory();
        return  check0BjFactory.checkObj(obj,nameList);
    }
    /**
     * 指定属性名检测非空
     * @param obj
     * @param params
     * @return 返回
     */
    public  String checkNotBlankObj(Object obj, Map<String,String> params){
        Class<?> clazz = obj.getClass();
        try {
            for (Field declaredField : clazz.getDeclaredFields()) {
                declaredField.setAccessible(true);
                if (params.containsKey(declaredField.getName())){
                    Object  val = declaredField.get(obj);
                    String name = declaredField.getName();
                    if (StringUtil.isNotBlank(params.get(name))){
                        name = params.get(name);
                    }
                    if (Objects.isNull(val)){
                        return  name+"不能为空";
                    }
                }
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return null;
    }


    /**
     * 检测对象值
     * @param obj
     * @return 返回
     */
  public  String checkObj(Object obj){
     Class<?> clazz = obj.getClass();
      try {
             for (Field declaredField : clazz.getDeclaredFields()) {
                 declaredField.setAccessible(true);
                Column column = declaredField.getAnnotation(Column.class);
                if (column!=null){
                   if (column.isCheck()){
                       Object  val = declaredField.get(obj);
                       String name = declaredField.getName();
                      if (StringUtil.isNotEmpty(column.comment())){
                         name = column.comment();
                      }
                      if (Objects.isNull(val)){
                         return  name+"不能为空";
                      }
                      if (StringUtil.isBlank(val.toString())){
                         return  name+"不能为空";
                      }
                      if (column.checkReg()!=null){
                          System.out.println("---->"+column.checkReg().length);
                         if (column.checkReg().length!=0){
                             String[] regs = column.checkReg();
                             boolean reg = reg(regs, val);
                             if (!reg){
                                 return name+"不合法";
                             }
                         }
                      }
                   }
                }
            }
      } catch (IllegalAccessException e) {
          throw new RuntimeException(e);
      }
     return null;
  }

    /**
     * 指定属性名检测
     * @param obj
     * @param nameList
     * @return 返回
     */
    public  String checkObj(Object obj, List<String> nameList){
        if (nameList==null){
            return null;
        }
        if (nameList.size()==0){
            return null;
        }
        Set<String> names = new HashSet<>(nameList);
        Class<?> clazz = obj.getClass();
        try {
            for (Field declaredField : clazz.getDeclaredFields()) {
                declaredField.setAccessible(true);
                String name = declaredField.getName();
                if (!names.contains(name)){
                    continue;
                }
                Column column = declaredField.getAnnotation(Column.class);
                if (column!=null){
                    if (column.isCheck()){
                        Object  val = declaredField.get(obj);
                        if (StringUtil.isNotEmpty(column.comment())){
                            name = column.comment();
                        }
                        if (Objects.isNull(val)){
                            return  name+"不能为空";
                        }
                        if (StringUtil.isBlank(val.toString())){
                            return  name+"不能为空";
                        }
                        if (column.checkReg()!=null){
                            if (column.checkReg().length!=0){
                                String[] regs = column.checkReg();
                                boolean reg = reg(regs, val);
                                if (!reg){
                                    return name+"不合法";
                                }
                            }
                        }
                    }
                }
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return null;
    }




    /**
     * 正则表达
     * @param regs
     * @param obj
     * @return 返回
     */
    public boolean reg(String[] regs, Object obj) {
        for (String reg : regs) {
            if (StringUtil.isBlank(reg)){
                continue;
            }
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(obj.toString());
            if (!matcher.matches()) {
                return false; // 只要有一个正则匹配，立即返回false
            }
        }
        return true; // 全部正则都不匹配时返回true
    }

    @Override
    public String getTableFactoryName() {
        return "请求参数校验工厂";
    }

    @Override
    public List<String> getSqls(Map<String, List<com.excu.entity.Column>> map) {
        return null;
    }

    @Override
    public void initData(String ps) {
        System.out.println(getTableFactoryName()+"初始化！");
    }
}
